package com.hdax.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.hdax.common.vaild.AddGroup;
import com.hdax.common.vaild.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hdax.gulimall.product.entity.PmsBrandEntity;
import com.hdax.gulimall.product.service.PmsBrandService;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.R;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;


/**
 * 品牌
 *
 * @author xiaohou
 * @email s2687781461@qq.com
 * @date 2023-04-07 17:41:13
 */
@RestController
@RequestMapping("product/brand")
public class PmsBrandController {
    @Autowired
    private PmsBrandService pmsBrandService;




    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:pmsbrand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pmsBrandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:pmsbrand:info")
    public R info(@PathVariable("brandId") Long brandId){
		PmsBrandEntity pmsBrand = pmsBrandService.getById(brandId);

        return R.ok().put("pmsBrand", pmsBrand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:pmsbrand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody PmsBrandEntity pmsBrand /*BindingResult bindingResult*/){
//
//        //判断校验是否成功
//        if(bindingResult.hasErrors()){
//            Map<String,String > map=new HashMap<>();
//            //1、获取校验的错误结果
//            //获取所有校验错误的字段
//            bindingResult.getFieldErrors().forEach((item)->{
//                    //错误提示消息
//                String defaultMessage = item.getDefaultMessage();
//                //获取发生错误的属性名称
//                String field = item.getField();
//                map.put(field,defaultMessage);
//            });
//        return  R.error(500,"提交数据不合法").put("data",map);
//        }else{
            pmsBrandService.save(pmsBrand);
//        }

        return R.ok();
    }

    /**
     * 修改，由于修改了品牌名称的同时，品牌关联的表的字段也得改变
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:pmsbrand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody PmsBrandEntity pmsBrand){
		pmsBrandService.updateByIdDetail(pmsBrand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:pmsbrand:delete")
    public R delete(@RequestBody Long[] brandIds){
		pmsBrandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }


    @RequestMapping("/lala")
    public String haha(){
        PmsBrandEntity pmsBrand=new PmsBrandEntity();


        //添加
        pmsBrand.setDescript("苹果");
        boolean save = pmsBrandService.save(pmsBrand);

        //修改
//        pmsBrand.setBrandId(6L);
//        pmsBrand.setDescript("苹果1");
//        boolean update = pmsBrandService.updateById(pmsBrand);


        //查询
//        List<PmsBrandEntity> brand_id = pmsBrandService.list(new QueryWrapper<PmsBrandEntity>().eq("brand_id", 1L));
//        for (PmsBrandEntity pmsBrandEntity : brand_id) {
//            System.out.println(pmsBrandEntity.getDescript());
//        }

        //删除
        boolean b = pmsBrandService.removeById(6L);

        return "ok";
    }

}
